import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import HomeView from "../views/HomeView.vue";

const modulesRoutes: Array<RouteRecordRaw> = [];

// 自动加载路由配置
const modulesFiles = require.context("./routes", false, /.+\.ts$/);
modulesFiles.keys().forEach((modulePath: string) => {
  modulesFiles(modulePath).default && modulesRoutes.push(...modulesFiles(modulePath).default);
});

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "home",
    component: HomeView,
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },
  ...modulesRoutes,
  {
    path: "/:pathMatch(.*)*",
    component: () => import("@/views/NotFoundPage.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.VUE_APP_WEB_BASE_PATH),
  routes,
});

export default router;
